﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Security;
using System.Security.Permissions;
using System.IO;

namespace SipII
{
    partial class SocketServer
    {
        /// <summary>
        /// 日志对象
        /// </summary>
        public static LogWritor logWritor = new LogWritor();

        static Queue<string> logQueue = new Queue<string> { };

        static void Main(string[] args)
        {
            logWritor.Output(string.Format("Sip2Server for {0} is startting ...", Properties.Settings.Default.ILSName));

            // 为访问限制创建 SocketPermission 对象
            SocketPermission permission = new SocketPermission(
                NetworkAccess.Accept,     // Allowed to accept connections
                TransportType.Tcp,        // Defines transport types
                "",                       // The IP addresses of local host
                SocketPermission.AllPorts // Specifies all ports
                );

            // 监听套接字对象
            Socket sListener = null;

            try
            {
                // 确保代码获得访问套接字的权限
                permission.Demand();

                // 将主机名解析为 IPHostEntry 实例
                IPHostEntry ipHost = Dns.GetHostEntry("");

                // 获得本机的第一个 IP 地址
                IPAddress ipAddr = ipHost.AddressList[0];

                // 创建网络地址标识
                IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, Properties.Settings.Default.ListeningPort);

                // 创建一个套接字对象来侦听连接
                sListener = new Socket(
                    ipAddr.AddressFamily,
                    SocketType.Stream,
                    ProtocolType.Tcp
                    );

                // 将套接字绑定到本地建网络地址标识
                sListener.Bind(ipEndPoint);

                // 将套接字设置为侦听状态并指定挂起连接队列的最大长度
                sListener.Listen(Properties.Settings.Default.ConnectionQueueLength);

                logWritor.Output(string.Format("Waiting for a connection on port {0}", ipEndPoint));

                // 开始一个异步操作来接受一个传入的连接尝试
                AsyncCallback aCallback = new AsyncCallback(AcceptCallback);
                sListener.BeginAccept(aCallback, sListener);
            }
            catch (Exception ex)
            {
                logWritor.Output(string.Format("Exception: {0}", ex.ToString()));
                logWritor.Clear();
                return;
            }

            logWritor.Output(string.Format("Sip2Server for {0} is running ...", Properties.Settings.Default.ILSName));

            Console.WriteLine("Press the Enter key to exit ...");
            Console.ReadLine();

            if (sListener.Connected)
            {
                sListener.Shutdown(SocketShutdown.Receive);
                sListener.Close();
            }

            logWritor.Output(string.Format("Sip2Server for {0} has exitted", Properties.Settings.Default.ILSName));
            logWritor.Clear();
        }

    }
}
